﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; 

namespace PrintClient.Controllers
{
    public class InputData {
        public string json { get; set; }
    }

    [Route("api/[controller]")]
    [ApiController]
    public class PrintController : ControllerBase
    {
         
        private readonly IConfiguration _configuration;
        public PrintController(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        // POST api/values
        [HttpPost] 
        public string Post(InputData data )
        {
            //var imgFilePath =AppContext.BaseDirectory+"\\"+ this._configuration["ImgPath"]+"\\"+ wayNo;
            var printUrl = this._configuration["PrintUrl"] + "";
            Console.WriteLine($"url:{printUrl},json{data.json}");
          return  TestCallWaybillPrinter.postJson(printUrl, data.json);
            //TestCallWaybillPrinter.WayBillPrinterTools(json, printUrl);
        }

        //将图片文件写入本地
        public static Boolean generateImage(string imgStr,string imgFilePath)
        {
            if (imgStr == null)
                return false;
            try
            {

                byte[] bytes = Convert.FromBase64String(imgStr);


                int x = 256;
                byte a = (byte)x;

                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] < 0)
                    {
                        bytes[i] += a;
                    }
                }

                //FileStream write = File.OpenWrite(imgFilePath);

                using (FileStream fs = new FileStream(imgFilePath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Write(bytes, 0, bytes.Length);
                    //fs.Flush();

                    fs.Close();
                }


            }
            catch (Exception e)
            {
                return false;
            }
            return true;

        }

    }
}
